package com.java.common.base.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import com.java.common.config.ConfigurationManager;

/**
 * 翻页用的基础域对象 Title:DefaultPageBean<br>
 * Description:<br>
 * Company: IBM<br>
 * Copyright @ 2012 .All rights reserved. <br>
 * @author Liuhl<br>
 * @version 2006-6-15 1.0
 */
public class DefaultPageBean extends DefaultSearchBean implements PageBean {

	protected int first;// 第一页

	protected int last;// 最后一页

	protected int previous;// 上一页

	protected int next;// 下一页

	protected int current;// 当前页

	protected int pageCount;// 总页数

	protected int rowCount; // 每页数据条数

	protected int start;// 起始记录位置

	protected int end;// 结束记录位置

	protected int maxResult;// 当次查询的最大记录数

	protected int count;// 记录总数

	protected Collection<Integer> pageNumList; // 存放页码列表供前端显示

	protected int pageListCount; // 每页显示的带有链接的页码数目

	protected int previousBlock; // 前一个页码列表起始页码

	protected int nextBlock; // 后一个页码列表起始页码
	
	/**
	 * 初始化成员变量
	 */
	public DefaultPageBean() {
		String rowCountStr = ConfigurationManager.getString("rowCount","10");
		first = 1;
		last = 1;
		previous = 1;
		next = 1;
		current = 1;
		pageCount = 1;
		rowCount = Integer.parseInt(rowCountStr);
		start = 1;
		end = 1;
		count = 0;
		pageNumList = new ArrayList<Integer>();
		pageListCount = 10;
		previousBlock = 1;
		nextBlock = 1;
		maxResult=1;
	}

	public int getCount() {
		return count;
	}

	public void setCount(int count) {
		this.count = count;
		this.countPage(count);
	}

	public int getCurrent() {
		return current;
	}

	public void setCurrent(int current) {
		this.current = current<1?1:current;
	}

	public int getEnd() {
		return end;
	}

	public int getFirst() {
		return first;
	}

	public int getLast() {
		return last;
	}

	public void setLast(int last) {
		this.last = last;
	}

	public int getNext() {
		return next;
	}

	public void setNext(int next) {
		this.next = next;
	}

	public int getNextBlock() {
		return nextBlock;
	}

	public void setNextBlock(int nextBlock) {
		this.nextBlock = nextBlock;
	}

	public int getPageCount() {
		return pageCount;
	}

	public void setPageCount(int pageCount) {
		this.pageCount = pageCount;
	}

	public int getPageListCount() {
		return pageListCount;
	}

	public void setPageListCount(int pageListCount) {
		this.pageListCount = pageListCount;
	}

	public Collection<Integer> getPageNumList() {
		return pageNumList;
	}

	public void setPageNumList(Integer[] pages) {
		this.pageNumList.addAll(Arrays.asList(pages));
	}

	public int getPrevious() {
		return previous;
	}

	public void setPrevious(int previous) {
		this.previous = previous;
	}

	public int getPreviousBlock() {
		return previousBlock;
	}

	public void setPreviousBlock(int previousBlock) {
		this.previousBlock = previousBlock;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getMaxResult() {
		return maxResult;
	}

	public void setMaxResult(int maxResult) {
		this.maxResult = maxResult;
	}

	private void countPage(int querySize) {
		//如果pageCount小于零则不做分页计算
		if(this.pageCount<0)return;
		this.pageCount = (int) Math.ceil((double) querySize / this.rowCount);// 得到总页数

		this.current = this.current < this.pageCount ? this.current : pageCount;// 设置当前页
		this.next = this.current + 1 < this.pageCount ? this.current + 1
				: pageCount;// 设置下一页
		this.previous = this.current - 1 > 0 ? this.current - 1 : 1;// 设置上一页

		this.first = 1;// 设置第一页
		this.last = pageCount + 1;// 设置末页
		Collection<Integer> col = new ArrayList<Integer>();

		// 每页显示的带有链接的页码数目的开始数
		int pageIdStart = this.pageCount > this.pageListCount
				&& this.current + this.pageListCount > this.pageCount ? this.pageCount
				- this.pageListCount
				: this.current;

		// 如果开始数小于列表的总数，就用当前页数，否则用开始数
		pageIdStart = pageIdStart < this.pageListCount ? this.current
				: pageIdStart;
		// 每页显示的带有链接的页码数目的结束数
		// 如果开始数加上列表总数大于了总页数那么结束的页码数就是列表总数
		int pageIdEnd = pageIdStart + this.pageListCount > this.pageCount ? this.pageCount
				: pageIdStart + this.pageListCount;
		pageIdEnd = pageIdEnd - pageIdStart < this.pageListCount ? pageIdEnd + 1
				: (pageIdEnd == this.last - 1 ? pageIdEnd + 1 : pageIdEnd);

		pageIdStart = pageIdEnd - pageIdStart < this.pageListCount ? this.pageCount
				- this.pageListCount + 1
				: pageIdStart;
		pageIdStart = pageIdEnd == this.last ? pageIdStart + 1 : pageIdStart;
		// 如果开始数小于1就让它等于1,
		pageIdStart = pageIdStart < 1 ? 1 : pageIdStart;
		for (int pb = pageIdStart; pb < pageIdEnd; pb++) {
			Integer pageId=new Integer(pb);
			col.add(pageId);
		}
		this.pageNumList = col;
		// 计算起始记录号
		this.start = (current - 1 < 0 ? 0 : current - 1) * rowCount;
		// 计算结束记录号
		this.end = (start + ((current + rowCount) > rowCount ? rowCount
				: (current + rowCount)));
		this.maxResult=rowCount<(count-(current-1)*rowCount)?rowCount:count-(current-1)*rowCount;
	}
}
